﻿using RStudio.UIFramwork.Utils;
using UIFramwork.Factory;
using UnityEngine;


namespace RStudio.UIFramwork
{
    /// <summary>
    /// 处理器|遮罩 
    /// </summary>
    public class HandlerUseMask : IUIHandler, IUIAcquisition
    {
        public IUIHandler Next { get; set; }
        public void Handle(UIBase uiBase)
        {
            if (uiBase.UseMask)
            {
                if (IsAcquisition)
                {
                    GameObject mask = UIComponentFactoryUtil.GetMask();
                    mask.transform.SetParent(uiBase.transform);

                    LayoutUtil.SetTransform(mask.transform);
                    LayoutUtil.SetSibling(mask.transform, 0);
                    LayoutUtil.SetAnchor(mask.transform, Vector2.zero, Vector2.one);
                    LayoutUtil.SetOffset(mask.transform, Vector2.zero, Vector2.zero);
                }
                else
                {
                    UIComponentFactoryUtil.Recycle<UIMask>(uiBase.transform.GetComponentInChildren<UIMask>().gameObject);
                }

            }

            Next?.Handle(uiBase);
        }



        public bool IsAcquisition { get; private set; }
        public HandlerUseMask(bool isAcquisition = true)
        { 
            this.IsAcquisition = isAcquisition;    
        }
    }
}